<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8M8MachineProForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    M8MachinePro,
    m8MachineProSave,
    m8MachineProForm,
  } from '/@/api/m8/machine/pro/m8MachinePro';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('m8.m8MachinePro');
  const { showMessage } = useMessage();
  const record = ref<M8MachinePro>({} as M8MachinePro);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增机床参数设置') : t('编辑机床参数设置'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('参数类型'),
      field: 'ctype',
      component: 'Select',
      componentProps: {
        dictType: 'm8_machine_proType',
        allowClear: true,
      },
      rules: [
        { required: true },
        { pattern: /^[a-zA-Z0-9_]*$/, message: t('请输入字母数字下划线') },
      ],
    },
    {
      label: t('数据来源'),
      field: 'csource',
      component: 'Select',
      componentProps: {
        dictType: 'M8_machine_proSource',
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('来源属性'),
      field: 'cfield',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('最小值'),
      field: 'minVal',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
      required: true,
    },
    {
      label: t('最大值'),
      field: 'maxVal',
      component: 'Input',
      componentProps: {
        maxlength: 100,
      },
    },
    {
      label: t('对应参数'),
      field: 'refCode',
      component: 'Select',
      componentProps: {
        dictType: 'm8_machine_proType',
        allowClear: true,
      },
    },
    {
      label: t('备注'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await m8MachineProForm(data);
    record.value = (res.m8MachinePro || {}) as M8MachinePro;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        ctype: record.value.ctype,
      };
      // console.log('submit', params, data, record);
      const res = await m8MachineProSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
